package com.myway.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;


/**
 * @ClassName: AmountUtils <br/>
 * @Description: This is the utils class for amount <br/>
 * @CreateDate: 2016年4月27日 下午4:20:54 <br/>
 * @author Toby
 * @version V1.0
 */
public class AmountUtils {

	static DecimalFormat myFormatter = new DecimalFormat("0");
	
	/**
	 * This method converts Big Decimal amount to amount string
	 *  
	 * @param amountBd
	 * @return
	 */
	public static String convertBigDecimalToAmountStr(BigDecimal amountBd) {
		String amountStr = "";
		
		if(amountBd!=new BigDecimal("0")) {
			amountStr = myFormatter.format(amountBd.multiply(new BigDecimal(100)));
		}
		
		return amountStr;
	}
	
	public static String convertBigDecimalToAmountStr(BigDecimal amountBd,String format) {
		DecimalFormat formatter=new DecimalFormat(format);
		String amountStr = formatter.format(amountBd.doubleValue());
		
		return amountStr;
		
	}
	
	public static String convertDoubleToAmountStr(double amountDb) {
		
		if("0.0".equals( String.valueOf( amountDb ) ))
		{
			return "0";
		}
		if("0.0010".equals( String.valueOf( amountDb ) ))
		{
			return "";
		}
		String amountStr = "0";
		
		if(amountDb>0) {
			amountStr = myFormatter.format(amountDb*100);
		}
		
		return amountStr;
	}
	
	/**
	 * This method convert amount str with 2 decimal places to double
	 * 
	 * @param amount
	 * @return
	 */
	public static double convertAmountStrToDouble(String amount) {
		
		double amountDb = 0.0;
		
		if(amount!=null && !"".equalsIgnoreCase(amount)) {
			amountDb = Double.parseDouble(amount)/100;
		}
		
		return amountDb;
	}
	
	/**
	 * This convert amount str with 2 decimal places to big decimal
	 * 
	 * @param amount
	 * @return
	 */
	public static BigDecimal convertAmountStrToBigDecimal(String amount) {
		
		DecimalFormat myFormatter = new DecimalFormat("#.00");
		BigDecimal amountBd = new BigDecimal("0");
		
		if(amount!=null) {
			amountBd = new BigDecimal(amount).divide(new BigDecimal(100));
			amountBd = new BigDecimal(myFormatter.format(amountBd));
		}
		
		return amountBd;
	}
	
	/**
	 * @名称：formatFlightAmount 
	 * @描述：航班价格四舍五入取整 
	 * @param amount
	 * @return
	*/
	public static BigDecimal formatFlightAmount(double amount){
		BigDecimal am=new BigDecimal(amount);
		am=am.setScale(-1, RoundingMode.HALF_UP);
		return am;
	}
	
	public static void main(String[] args ){
//		up=四舍五入
//		down=五会舍去，大于5就入
		BigDecimal a=new BigDecimal(20.445);
		System.out.println(a.setScale(-1, RoundingMode.HALF_UP).doubleValue());
		String paymentAmountStr = AmountUtils.convertBigDecimalToAmountStr(new BigDecimal(20.544).setScale(-1, RoundingMode.HALF_UP),"#0.00");
		System.out.println(paymentAmountStr);
		System.out.println(formatFlightAmount(170006.0).doubleValue());
		
	}
	
}
